<?php

/*
 *
 *
 *  This software has been made FREE by author for the interest of other developers.
 *       I hope this will help you in coding faster and robust php code
 *             You can modify and redistribute it as you like.
 *               Your comments, suggestions and corrections
 *                           will be highly
 *                            APPRECIATED.
 *                                       Author
 * @author :   Roshan Budhathoki (roshan_bnfm@yahoo.com);
 * @URL    :   <http://www.roshanbudhathoki.com>
 *
 */

/**
 * Description of ImageResize
 *
 * @author Roshan Budhathoki (roshan_bnfm@yahoo.com);
 */
class Image_Resize {

    //put your code here
    var $image;
    var $image_type;
    var $image_save_path;
    var $image_save_name;
    var $image_tmp_name;
    var $image_original_height;
    var $image_original_width;
    public static $instance;

    public static function newInstance() {
        if (!self::$instance instanceof self) {
            self::$instance = new self ();
        }
        return self::$instance;
    }
    

    function load($filename) {
        $image_info = getimagesize($filename);
        $this->image_tmp_name = $filename;
        $this->image_type = $image_info[2];
        if ($this->image_type == IMAGETYPE_JPEG) {
            $this->image = imagecreatefromjpeg($filename);
        } elseif ($this->image_type == IMAGETYPE_GIF) {
            $this->image = imagecreatefromgif($filename);
        } elseif ($this->image_type == IMAGETYPE_PNG) {
            $this->image = imagecreatefrompng($filename);
        }
        $this->image_original_height = $this->getHeight();
        $this->image_original_width = $this->getWidth();
    }

    function save($filename, $image_type = '', $compression = 100, $permissions = null) {

        if ($image_type == '') {
            $image_type = $this->image_type;
        }


        if ($image_type == IMAGETYPE_JPEG) {
            imagejpeg($this->image, $filename, 100);
        } elseif ($image_type == IMAGETYPE_GIF) {
            imagegif($this->image, $filename);
        } elseif ($image_type == IMAGETYPE_PNG) {
            if ($this->getHeight() == $this->image_original_height || $this->getWidth() == $this->image_original_width) {
                $this->resize($this->getWidth(), $this->getHeight());
            }
            imagepng($this->image, $filename, 9, PNG_ALL_FILTERS);
        }
        if ($permissions != null) {
            chmod($filename, $permissions);
        }
    }

    function output($image_type = IMAGETYPE_JPEG) {
        if ($image_type == IMAGETYPE_JPEG) {
            imagejpeg($this->image);
        } elseif ($image_type == IMAGETYPE_GIF) {
            imagegif($this->image);
        } elseif ($image_type == IMAGETYPE_PNG) {
            imagepng($this->image);
        }
    }

    function getWidth() {
        return imagesx($this->image);
    }

    function getHeight() {
        return imagesy($this->image);
    }

    function resizeToHeight($height) {
        if ($height < $this->getHeight()) {
            $ratio = $height / $this->getHeight();
            $width = $this->getWidth() * $ratio;
            $this->resize($width, $height);
        }
    }

    function resizeToWidth($width) {
        if ($width < $this->getWidth()) {
            $ratio = $width / $this->getWidth();
            $height = $this->getheight() * $ratio;
            $this->resize($width, $height);
        }
    }

    function scale($scale) {
        $width = $this->getWidth() * $scale / 100;
        $height = $this->getheight() * $scale / 100;
        $this->resize($width, $height);
    }

    function resize($width, $height) {
        $new_image = imagecreatetruecolor($width, $height);

        if ($this->image_type == IMAGETYPE_GIF) {
            imagealphablending($new_image, false);
            imagesavealpha($new_image, true);
            $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
            imagefilledrectangle($new_image, 0, 0, $this->getWidth(), $this->getHeight(), $transparent);
            imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        } else if ($this->image_type == IMAGETYPE_JPEG) {
            imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        } else {
            // its png now process it
            $sourcemg = $this->image_tmp_name;
            $s = getimagesize($sourcemg);
            $w = $width;
            $h = $height;
            $source = imagecreatefrompng($sourcemg);
            $new_image = imagecreatetruecolor($w, $h);

            $palette = (imagecolortransparent($source) < 0);

            if (!$palette || (ord(file_get_contents($sourcemg, false, null, 25, 1)) & 4)) {
                // print ("Is Alpha");
                // Has indexed transparent color
                if (($tc = imagecolorstotal($source)) && $tc <= 256)
                    imagetruecolortopalette($new_image, false, $tc);
                imagealphablending($new_image, false);
                $alpha = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
                imagefill($new_image, 0, 0, $alpha);
                imagesavealpha($new_image, true);
            }

            imagecopyresampled($new_image, $source, 0, 0, 0, 0, $w, $h, $s[0], $s[1]);
            if ((ord(file_get_contents($sourcemg, false, null, 25, 1)) & 4)) {
                // print ("Testing is there is Alpha transparency");
                // we dont have to check every pixel.
                // We take a sample of 2500 pixels (for images between 50X50 up to 500X500), then 1/100 pixels thereafter.
                $new_imagex = min(max(floor($w / 50), 1), 10);
                $new_imagey = min(max(floor($h / 50), 1), 10);

                $palette = true;
                for ($x = 0; $x < $w; $x = $x + $new_imagex) {
                    for ($y = 0; $y < $h; $y = $y + $new_imagey) {
                        $col = imagecolorsforindex($new_image, imagecolorat($new_image, $x, $y));
                        // How transparent until it's actually visible
                        // I reackon atleast 10% of 127 before its noticeable, e.g. ~13
                        if ($col['alpha'] > 13) {
                            //print_r($col);
                            $palette = false;
                            break 2;
                        }
                    }
                }
            }
            if ($palette) {
                //print "Converting to indexed colors";
                imagetruecolortopalette($new_image, false, 256);
            }
        }

        $this->image = $new_image;
    }

}

